import {defineStore} from "pinia";

const dict = defineStore('dict', {
    state: () => ({
        dict: [] as any,
    }),
    actions: {
        //获取字典
        getDict(_key: string | null){
            if (_key === null || _key === ""){
                return null;
            }
            for (let i = 0; i < this.dict.length; i++) {
                if (this.dict[i].key === _key) {
                    return this.dict[i].value;
                }
            }
        },
        //设置字典
        setDict(_key: string | null, value: any){
            if (_key !== null && _key !== ""){
                this.dict.push({
                    key: _key,
                    value: value
                })
            }
        },
        //移除字典
        removeDict(_key: string){
            for (let i = 0; i < this.dict.length; i++) {
                if (this.dict[i].key === _key) {
                    this.dict.splice(i, 1);
                    return true;
                }
            }
            return false;
        },
        //清空字典
        clearDict(){
            this.dict = [];
        }
    }
})


export default dict